/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.maze.behaviours;

import java.util.Random;
import org.maze.Maze;
import org.maze.individual.Individual;

/**
 *
 * @author Thomas
 */
public class ImprovedSegmentCrossBehaviour  implements ICrossBehaviour{
    private static final Random rand = new Random();

    public byte[][] cross(Individual father, Individual mother) {
        byte[] m = mother.getGenome();
        byte[] f = father.getGenome();
        byte[] child = new byte[Maze.SIZE*Maze.SIZE];
        byte[] child2 = new byte[Maze.SIZE*Maze.SIZE];

        int pos1;
        if(father.getPath().size()>mother.getPath().size())
            pos1 = father.getPath().size();
        else
            pos1 = mother.getPath().size();

        int pos2 = rand.nextInt(child.length-pos1)+pos1;
        int i;
        // copy mother gene between 0 and pos1
        for(i=0; i<pos1; i++){
            child[i] = m[i];
            child2[i] = f[i];
        }
        // copy father gene between pos1 and pos2
        for(;i<pos2;i++){
            child[i]=f[i];
            child2[i] = m[i];
        }
        // copy mother gene between pos2 and size
        for(;i<child.length;i++){
            child[i]=m[i];
            child2[i] = f[i];
        }
        byte[][] results = new byte[2][];
        results[0]=child;
        results[1]=child2;
        return results;
    }

}
